package cc.autoapi.pucong.auto.flow.spring.boot.start.extTask;


import cc.autoapi.pucong.auto.flow.core.flow.execNode.ai.send.AbstractAiSendSendTool;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.ExecParamIn;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.NodeParamIn;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.NodeParamOut;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.selfenum.DataTypeEnum;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.task.extend.AbstractExtendTask;
import cc.autoapi.pucong.auto.flow.core.root.utils.ApplicationContextUtil;
import com.alibaba.fastjson2.JSONArray;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class AiChatTask extends AbstractExtendTask {

    private List<NodeParamIn> paramsIns;
    private List<NodeParamOut> paramsOuts;
    private JSONArray messages = new JSONArray();

    public AiChatTask() {
        paramsIns = new ArrayList<>();
        NodeParamIn system = new NodeParamIn()
                .setParamCode("system")
                .setDateType(DataTypeEnum.TXT)
                .setDesc("系统提示词")
                .setNeedValue(false);
        paramsIns.add(system);
        NodeParamIn question = new NodeParamIn()
                .setParamCode("question")
                .setDateType(DataTypeEnum.TXT)
                .setDesc("问题内容")
                .setNeedValue(true);
        paramsIns.add(question);

        paramsOuts = new ArrayList<>();
        NodeParamOut msg = new NodeParamOut()
                .setParamCode("msg")
                .setDesc("ai回答结果")
                .setDateType(DataTypeEnum.STRING);
        paramsOuts.add(msg);
    }

    @Override
    public void run() {
        ExecParamIn system = getParamInInfo("system");
        Object systemObj = system.getDateValue();

        ExecParamIn question = getParamInInfo("question");
        Object questionObj = question.getDateValue();
        String questionStr = questionObj.toString();

        AbstractAiSendSendTool sendSendTool = (AbstractAiSendSendTool) ApplicationContextUtil.getBean(AbstractAiSendSendTool.class);
        if (systemObj != null) {
            String systemStr = systemObj.toString();
            sendSendTool.initSystem(systemStr);
        }

        String s = sendSendTool.sendToAi(questionStr);
        setParamOutValue("msg", s);
    }

    @Override
    public String getTaskName() {
        return "ai对话";
    }

    @Override
    public String getTaskDesc() {
        return "本组件是可以进行ai对话,返回AI的回答结果。system是提示词，question是用户的问题，msg是AI输出的内容。";
    }

    @Override
    public String getTaskSvgIcon() {
        return "<svg t=\"1731424447395\" class=\"icon\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"20078\" width=\"32\" height=\"32\"><path d=\"M362.872073 227.229389h285.47386c187.474908 0 340.864834 153.388925 340.864834 340.864834v340.863833c0 63.911969-51.129975 115.041944-115.041944 115.041944h-737.11964c-63.911969 0-115.041944-51.129975-115.041944-115.041944V568.094223c0-187.475908 153.389925-340.864834 340.864834-340.864834z\" fill=\"#73FCD2\" p-id=\"20079\"></path><path d=\"M418.262046 342.270333h170.432917c157.649923 0 285.473861 127.824938 285.47386 285.47486v170.431917c0 63.911969-51.129975 115.041944-115.041944 115.041944H247.830129c-63.911969 0-115.041944-51.129975-115.041944-115.041944V627.745193c4.259998-157.649923 132.084936-285.473861 285.473861-285.47386z\" fill=\"#14254E\" p-id=\"20080\"></path><path d=\"M371.393069 721.483148c12.782994-8.521996 29.825985-8.521996 38.347981 0 21.30399 25.563988 51.129975 34.085983 85.215958 34.085983s59.650971-12.781994 80.954961-34.085983c8.521996-12.782994 29.825985-12.782994 38.346981-4.260998 12.782994 8.520996 12.782994 29.824985 4.260998 38.346981-29.824985 38.346981-72.433965 55.389973-123.56294 55.389973s-93.737954-17.042992-127.823937-55.389973c-8.521996-4.259998-8.521996-21.30399 4.259998-34.085983z\" fill=\"#40E3C2\" p-id=\"20081\"></path><path d=\"M350.090079 478.617266c-42.608979 0-76.694963 34.085983-76.694962 76.693963s34.085983 76.694963 76.693962 76.694962 76.694963-34.086983 76.694963-76.694962-34.086983-76.693963-76.694963-76.693963z\" fill=\"#25C7A6\" p-id=\"20082\"></path><path d=\"M350.090079 512.70325c25.563988 0 42.606979 17.042992 42.606979 42.607979s-17.042992 42.607979-42.607979 42.607979-42.607979-17.042992-42.607979-42.607979 17.042992-42.607979 42.607979-42.607979z\" fill=\"#40E3C2\" p-id=\"20083\"></path><path d=\"M661.127927 478.617266c-42.607979 0-76.693963 34.085983-76.693962 76.693963s34.085983 76.694963 76.693962 76.694962 76.694963-34.086983 76.694963-76.694962-34.086983-76.693963-76.694963-76.693963z\" fill=\"#25C7A6\" p-id=\"20084\"></path><path d=\"M661.127927 512.70325c25.564988 0 42.607979 17.042992 42.607979 42.607979s-17.042992 42.607979-42.607979 42.607979-42.607979-17.042992-42.607979-42.607979 17.042992-42.607979 42.607979-42.607979z\" fill=\"#40E3C2\" p-id=\"20085\"></path><path d=\"M247.830129 115.042444h55.389973v170.431917h-55.389973z m455.905777 0h55.389973v170.431917h-55.389973z\" fill=\"#73FCD2\" p-id=\"20086\"></path><path d=\"M192.439156 85.216458a85.215958 85.215958 0 1 0 170.432917 0 85.215958 85.215958 0 1 0-170.432917 0z\" fill=\"#73FCD2\" p-id=\"20087\"></path><path d=\"M648.345933 85.216458a85.215958 85.215958 0 1 0 170.431917 0 85.215958 85.215958 0 1 0-170.431917 0z\" fill=\"#73FCD2\" p-id=\"20088\"></path></svg>";
    }

    @Override
    public List<NodeParamIn> getPropertyParamsIns() {
        return paramsIns;
    }

    @Override
    public List<NodeParamOut> getPropertyParamsOuts() {
        return paramsOuts;
    }
}
